import { game } from "../../game/control/game";
import { ccs } from "../../util/services";
import global from "../global";
import { GameState, PlayerData } from "../model/GameState";

// 帧广播消息缓存
let frames: MGOBE.types.Frame[] = [];

// 帧消息命令：跑、停
export enum FrameSyncCmd {
    run = 1,
    stop = 2,
}

export interface PlayerState {
    lastUpdateFrameId: number,
    hp: number,
    money: number,
}

// 帧同步逻辑状态
export const frameSyncState: GameState<PlayerState> = {
    players: []
};

export function clearFrames() {
    frames = [];
}

export function pushFrames(frame: MGOBE.types.Frame) {
    frames.push(frame);
}

// 重新从第一帧计算逻辑状态
export function reCalcFrameState(room: MGOBE.Room) {
    setDefauleFrameState(room);
    frames.forEach(frame => {
        calcFrame(frame);
    });
}

// 设置默认逻辑状态 初始化玩家
export function setDefauleFrameState(room: MGOBE.Room) {

    const roomInfo = room.roomInfo || { playerList: [] } as MGOBE.types.RoomInfo;
    frameSyncState.players = [];

    roomInfo.playerList.forEach((p, i) => {
        const player: PlayerData<PlayerState> = {
            x: 0,
            y: i,
            id: p.id,
            state: {
                hp: 100,
                money:p.customPlayerStatus,
                lastUpdateFrameId: 1,
            },
        };
        setDefaultPlayerState(player, p.id, i);
        frameSyncState.players.push(player);
    });
}
// 游戏玩家属性设置
function setDefaultPlayerState(player: PlayerData<PlayerState>, id: string, y: number) {
    player.id = id;
    player.x = 0;
    player.y = y;
    player.state.hp = player.state.hp;
    player.state.money = player.state.money;
    player.state.lastUpdateFrameId = 1;
}

const MAX_X = 18 - 1;
const MIN_X = 0;
const DELTA_FRAME = 1;
// 设置战斗逻辑
function setPlayerCMD(id: string, data: any) {
    if(data.playerId) {
        id = data.playerId;
    }
    let player = frameSyncState.players.find(p => p.id === id) || { state: {} } as PlayerData<PlayerState>;
    for(let key in data) {
        if(key === 'money') {
            player.state.money -= data[key];
            ccs.mainEmitter.emit('attack', id, data[key]);
        } else if(key === 'hp'){
            player.state.hp -= data[key];
            if(player.state.hp <= 0) {
                ccs.mainEmitter.emit("youDie", id);
            }
        }
    }
    ccs.mainEmitter.emit('upInfo');
}
export function hurt(id) {
    
    let player = frameSyncState.players.find(p => p.id === id) || { state: {} } as PlayerData<PlayerState>;
    player.state.hp --;
    if(player.state.hp <= 0) {
        ccs.mainEmitter.emit("youDie", id);
    }
    ccs.mainEmitter.emit('upInfo');
}
//时间到了结算游戏
export function calResult() {
    let list = frameSyncState.players;
    list.sort((a, b) => {
        return b.state.hp - a.state.hp;
    });
    return list[0];
}
// 根据命令字更新玩家状态
function calcPlayerState(player: PlayerData<PlayerState>, frameId: number) {
    if (player.state.hp === 0) {
        return;
    }

    if (frameId - player.state.lastUpdateFrameId > DELTA_FRAME) {
        player.state.lastUpdateFrameId = frameId;
        // player.state.hp --;
    }
}

// 根据每一帧计算游戏逻辑
export function calcFrame(frame: MGOBE.types.Frame) {
    if (frame.id === 1) {
        setDefauleFrameState(global.room);
    }

    if (frame.items && frame.items.length > 0) {
        frame.items.forEach(item => {
            setPlayerCMD(item.playerId, item.data);
        });
    }

    frameSyncState.players.forEach(player => calcPlayerState(player, frame.id));
}